package com.example.music.service.impl;

import com.example.music.service.MsmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.mail.internet.MimeMessage;

/**
 * @ClassName MsmServiceImpl
 * @Author: wx
 * @Create: 2022-10-29  19:58
 * @Description TODO
 * @Version: 1.0
 **/
@Service
public class MsmServiceImpl implements MsmService {

    @Autowired
    private JavaMailSender javaMailSender;

    //发送人
    private String from = "515127365@qq.com";
    //接收人
    private String to = "";
    //标题
    private String subject = "Xmusic";
    //正文
    private String context = "【Xmusic】 <br>您的验证码为：";

    @Override
    public boolean sendMail(String code,String qq){
        if (StringUtils.isEmpty(qq)){
            return false;
        }
        try {
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message,true);
            helper.setFrom(from + "(Xmusic)");
            helper.setTo(qq);
            helper.setSubject(subject);
            helper.setText(context+code+",2分钟内有效！",true);
            javaMailSender.send(message);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
